package com.freak.freakmusic.base

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

import com.freak.freakmusic.app.App
import com.freak.freakmusic.app.App.Companion.DESIGN_WIDTH
import com.freak.freakmusic.net.status.NetStateChangeObserver
import com.freak.freakmusic.net.status.NetStateChangeReceiver
import com.freak.freakmusic.net.status.NetworkType
import com.freak.freakmusic.utils.ToastUtil


/**
 * 无MVP的Fragment基类
 * @author Administrator
 * @date 2019/9/11.
 */

abstract class BaseSimpleFragment : Fragment(), NetStateChangeObserver {

    protected lateinit var mView: View
    protected lateinit var mActivity: Activity
    protected lateinit var mContext: Context
    private val netErrorView: View? = null

    protected abstract val layoutId: Int

    protected abstract fun initEventAndData(view: View)

    protected abstract fun initLazyData()

    @SuppressLint("MissingSuperCall")
    override fun onAttach(context: Context) {
        mActivity = context as Activity
        mContext = context
        super.onAttach(context)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        App.resetDensity(mActivity, DESIGN_WIDTH.toFloat())
        mView = inflater.inflate(layoutId, container, false)

        return mView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initEventAndData(view)
        initLazyData()
    }


    override fun onDestroyView() {
        super.onDestroyView()
        /**
         * 判断该页面是否开启了网络变化监听，已开启则关闭广播
         */
        if (needRegisterNetworkChangeObserver()) {
            NetStateChangeReceiver.unregisterObserver(this)
        }
    }

    override fun onResume() {
        super.onResume()
        /**
         * 判断该页面是否需要开启网络变化监听，如需要则启动广播
         */
        if (needRegisterNetworkChangeObserver()) {
            NetStateChangeReceiver.registerObserver(this)
        }
    }


    /**
     * 是否需要注册网络变化的Observer,如果不需要监听网络变化,则返回false;否则返回true
     */
    protected fun needRegisterNetworkChangeObserver(): Boolean {
        return true
    }

    /**
     * 网络断开时执行的操作
     */
    override fun onNetDisconnected() {
        showDisConnectedView()
    }

    /**
     * 网络重连时执行的操作
     *
     * @param networkType
     */
    override fun onNetConnected(networkType: NetworkType) {
        ToastUtil.showLong(mActivity, "当前连接的是" + networkType.toString() + "网络")
        hideDisConnectedView()
    }

    /**
     * 显示无网络状态
     */
    fun showDisConnectedView() {
        ToastUtil.shortShow("网络连接错误，请检测您的网络设置")
        //        netErrorView = mView.findViewById(R.id.rl_net_error);
        //        netErrorView.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏无网络状态
     */
    fun hideDisConnectedView() {
        //        netErrorView = mView.findViewById(R.id.rl_net_error);
        //        netErrorView.setVisibility(View.GONE);
    }
}